import request from "./http.api.js";

/**
 * 封装get2方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function get2(url, params = {}, loading) {
	if (loading === true) {
		uni.showLoading({
			title: "加载中...",
			mask: true
		});
	}
	return new Promise((resolve, reject) => {
		request.get(url, params)
			.then(res => {
				if (res.length > 1 && res[1].statusCode == 200) {
					resolve(res[1].data);
					return
				}
			})
			.catch(err => {
				uni.hideLoading();
				uni.showToast({
					title: "加载错误,请重试",
					icon: "none",
					duration: 2000
				});
			})
	})
}
/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function get(url, params = {}) {
	return new Promise((resolve, reject) => {
		request.get(url, params)
			.then(res => {
				if (res.length > 1 && res[1].statusCode == 200) {
					resolve(res[1].data);
					return
				} else {
					uni.hideLoading();
					uni.showToast({
						icon: 'none',
						title: '网络拥堵请重试',
						duration: 2000
					});
				}
			}, err => {
				uni.hideLoading();
				uni.showToast({
					icon: 'none',
					title: '网络拥堵请重试',
					duration: 2000
				});
			})
	})
}
/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url, data = {}) {
	return new Promise((resolve, reject) => {
		request.post(url, data)
			.then(res => {
				console.log(res)
				if (res.length > 1 && res[1].statusCode == 200) {
					resolve(res[1].data);
					return
				} else {
					uni.hideLoading();
					uni.showToast({
						icon: 'none',
						title: '网络拥堵请重试',
						duration: 2000
					});
				}
			}, err => {
				console.log(err)
				uni.hideLoading();
				uni.showToast({
					icon: 'none',
					title: '网络拥堵请重试',
					duration: 2000
				});
			})
	})
}
/**
 * 封装post请求 请求后不处理错误
 * @param url
 * @param data
 * @returns {Promise}
 */
export function postOnly(url, data = {}) {
	return new Promise((resolve, reject) => {
		request.post(url, data)
			.then(res => {
				console.log(res)
				if (res.length > 1) {
					resolve(res[1].data);
				} else {
					reject("")
				}
			}, err => {
				console.log(err)
				reject(err)
			})
	})
}
/**
 * 封装put方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function put(data, url) {
	return new Promise((resolve, reject) => {
		request.put(url, data)
			.then(res => {
				resolve(res)
			})
			.catch(err => {
				reject(err)
			})
	})
}
/**
 * 封装delete方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function del(data, url) {
	return new Promise((resolve, reject) => {
		request.del(url, data)
			.then(res => {
				console.log(res)
				if (res.length > 1) {
					resolve(res[1].data);
				}
			}, err => {
				console.log(err)
				reject(err)
			})
	})
}
/**
 * 封装上传方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function uploadFile(url, filePath, formData, fileNameKey) {
	return new Promise((resolve, reject) => {
		request.uploadFile(url, filePath, formData, fileNameKey)
			.then(res => {
				if (res.statusCode == 200) {
					resolve(JSON.parse(res.data));
				} else {
					uni.hideLoading();
					uni.showToast({
						icon: 'none',
						title: '网络拥堵请重试',
						duration: 2000
					});
				}
			}, err => {
				uni.hideLoading();
				uni.showToast({
					icon: 'none',
					title: '网络拥堵请重试',
					duration: 2000
				});
			})
	})
}
